package com.example.controller;

import com.example.entity.User;
import com.example.security.CustomUserDetails;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public String list(Model model) {
        List<User> users = userService.list();
        model.addAttribute("users", users);
        return "user/list";
    }

    @GetMapping("/form")
    public String form(@RequestParam(required = false) Long id, Model model) {
        User user = id != null ? userService.getById(id) : new User();
        model.addAttribute("user", user);
        return "user/form";
    }

    @PostMapping("/save")
    public String save(User user) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal();
        User currentUser = userDetails.getUser();
        
        if (user.getId() == null) {
            // 新增用户
            user.setCreatedBy(currentUser.getUsername());
            user.setCreatedAt(new Date());
            // 设置默认状态
            user.setEnabled(true);
            user.setAccountNonLocked(true);
            user.setAccountNonExpired(true);
            user.setCredentialsNonExpired(true);
            userService.save(user);
        } else {
            // 更新用户
            user.setUpdatedBy(currentUser.getUsername());
            user.setUpdatedAt(new Date());
            userService.update(user);
        }
        
        return "redirect:/user/list";
    }

    @GetMapping("/delete")
    public String delete(@RequestParam Long id) {
        userService.delete(id);
        return "redirect:/user/list";
    }

    @GetMapping("/toggle-status")
    public String toggleStatus(@RequestParam Long id, @RequestParam String type) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal();
        User currentUser = userDetails.getUser();
        
        User user = userService.getById(id);
        if (user != null) {
            switch (type) {
                case "enabled":
                    user.setEnabled(!user.getEnabled());
                    break;
                case "locked":
                    user.setAccountNonLocked(!user.getAccountNonLocked());
                    break;
                case "expired":
                    user.setAccountNonExpired(!user.getAccountNonExpired());
                    break;
                case "credentials":
                    user.setCredentialsNonExpired(!user.getCredentialsNonExpired());
                    break;
            }
            user.setUpdatedBy(currentUser.getUsername());
            user.setUpdatedAt(new Date());
            userService.save(user);
        }
        return "redirect:/user/list";
    }
}